var InterValObj; //timer变量，控制时间
var count = 60; //间隔函数，1秒执行
var curCount;//当前剩余秒数
// var phoneBtn = '#' + phoneCodeBtn;
$(function () {
    $("#phoneCodeBtn").click(function () {
        curCount = count;
        var phone = $("#phone").val();
        // console.log(phone)
        // console.log(phoneCodeBtn)
        if (checkPhone(phone)) {
            $.post(basePath + "/safety/oldPhone", {"phone": phone}, function (result) {
                if (result.code == 100) {
                    alert("确保手机号正确");
                }
                // console.log(result);
            });
            $(this).attr("disabled", "true");
            $(this).val(curCount + "s");
            InterValObj = window.setInterval(SetOldTime, 1000);//启动计时器，1秒执行一次
            //向后台发送处理数据

        }
    });


    $("#newPhone").click(function () {
        curCount = count;
        var phone = $("#user-new-phone").val();
        // console.log(phone)
        // console.log(phoneCodeBtn)
        if (checkPhone(phone)) {
            $.post(basePath + "/safety/newPhone", {"phone": phone}, function (result) {
                if (result.code == 100) {
                    alert("确保手机号正确");
                }
                // console.log(result);
            });
            $(this).attr("disabled", "true");
            $(this).val(curCount + "s");
            InterValObj = window.setInterval(SetNewTime, 1000);//启动计时器，1秒执行一次
            //向后台发送处理数据

        }
    });

    $("#changPhone").submit(function () {
        var oldPhone = $("#phone").val();
        var newPhone = $("#user-new-phone").val();
        var oldCode = $("#user-code").val();
        var newCode = $("#user-new-code").val();
        $.post(basePath + "/safety/changPhone", {
            "oldPhone": oldPhone,
            "newPhone": newPhone,
            "oldCode": oldCode,
            "newCode": newCode
        }, function (result) {
            if (result == 1) {
                alert("修改成功");
                window.location.href = basePath + "/user/index";
            } else {
                alert("验证码错误！");
            }
        });

    });
});


function SetOldTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $("#phoneCodeBtn").removeAttr("disabled");
        $("#phoneCodeBtn").val("重发");
    } else {
        curCount--;
        $("#phoneCodeBtn").val(curCount + "s");
    }
}

function SetNewTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        $("#newPhone").removeAttr("disabled");
        $("#newPhone").val("重发");
    } else {
        curCount--;
        $("#newPhone").val(curCount + "s");
    }
}

//正则校验
function checkPhone(phone) {
    if (!(/^1[3|4|5|7|8]\d{9}$/.test(phone))) {
        alert("请仔细核对自己的号码哦");
        return false;
    }
    return true;
}
